package com.fysports.web.controller.user;

import com.google.common.collect.Lists;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.fysports.web.controller.engine.AutoRestFulController;
import com.fysports.web.kit.cache.DictCacheKit;
import com.fysports.web.kit.dtgrid.DTGridView;
import com.fysports.web.model.user.UserAddress;
import com.fysports.web.module.view.JsonView;
import com.fysports.web.module.view.OperateType;

import java.util.List;


/**
 * Class title: 用户地址维护  <br/>
 * Describe:                    <br/>
 * Created by konbluesky           <br/>
 * Date : 15/12/25 下午5:48       <br/>
 * Project : weixin    <br/>
 */
public class UserAddressController extends AutoRestFulController<UserAddress> {

    /**
     * ajax调用获取各地区地址
     */
    public void get(){
        String aid=getPara(0,"");
        String sql="select id,areaname,parentid,shortname from dict_address where parentid=?";
        List<Record> list=Db.findByCache(DictCacheKit.TMPCACHE,"dict_address_"+aid,sql,aid);
        JsonView jv=new JsonView();
        if(list==null || list.isEmpty()){
            jv.setStatus(false).setMsg(OperateType.OPERATE_FAIL);
        }else{
            jv.setStatus(true).setMsg(OperateType.OPERATE_SUCCESS).setDataList(list);
        }
        renderJson(jv);
    }


    @Override public void load() {
        DTGridView view = getGridView();
        String sql="select ua.*,u.memberid ";
        String sqlext=" from user_address as ua left join user as u on u.uuid=ua.useruuid ";
        Page<UserAddress> list = UserAddress.dao.DTSearchPaginate(view.getNowPage(), view.getPageSize(), sql, sqlext, null, null);
        renderJson(toGridView(view, list));
    }


    /**
     * 根据用户信息查找该用户拥有的地址
     * users_update
     */
    public void loaduser() {
        String useruuid=getPara();
        DTGridView view = getGridView();
        String sql="select ua.*,u.memberid ";
        String sqlext=" from user_address as ua right join user as u on u.uuid=ua.useruuid where u.uuid=?";
        List<Object> params= Lists.newArrayList();
        params.add(useruuid);
        Page<UserAddress> list = UserAddress.dao.DTSearchPaginate(view.getNowPage(), view.getPageSize(), sql, sqlext, null, params);
        renderJson(toGridView(view, list));
    }

    @Override protected UserAddress getCtrModel() {
        return UserAddress.dao;
    }

    @Override protected String getRenderPath() {
        return "user/address/address_";
    }
}
